package AionCrafter.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Gui implements ActionListener, DocumentListener {
	
	public JFrame frame = new JFrame("Aion Crafter");
	BrowsePanel browsePanel = new BrowsePanel();
	SubComponentsPanel scPanel;
	
	JTextField taxRateField = new JTextField();
	JTextField serverPriceField = new JTextField();

	public Gui() {
		
		//##########################################
		//  Frame Setup
		//##########################################
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		//##########################################
		//  Menu Bar
		//##########################################
		JMenuBar menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(fileMenu);
		
		JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
		saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		fileMenu.add(saveMenuItem);
		
		JMenuItem saveAsMenuItem = new JMenuItem("Save As", KeyEvent.VK_A);
		fileMenu.add(saveAsMenuItem);
			
		JMenu toolsMenu = new JMenu("Tools");
		toolsMenu.setMnemonic(KeyEvent.VK_T);
		menuBar.add(toolsMenu);
		
		JMenuItem optionsMenuItem = new JMenuItem("Options", KeyEvent.VK_O);
		toolsMenu.add(optionsMenuItem);
		
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);
		menuBar.add(helpMenu);
		
		JMenuItem helpContentsMenuItem = new JMenuItem("Help Contents", KeyEvent.VK_H);
		helpContentsMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
		helpMenu.add(helpContentsMenuItem);
		
		JMenuItem aboutMenuItem = new JMenuItem("About", KeyEvent.VK_H);
		helpContentsMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
		helpMenu.add(helpContentsMenuItem);
		
		frame.setJMenuBar(menuBar);
		
		//##########################################
		//  Mode Chooser
		//##########################################
		JPanel modeChoosePanel = new JPanel();
		modeChoosePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		JButton alchemyModeButton = new JButton("Alchemy");
		alchemyModeButton.setMaximumSize(new Dimension(80,15));
		alchemyModeButton.setActionCommand("alchemy mode");
		alchemyModeButton.addActionListener(this);
		
		JButton armorModeButton = new JButton("Armor");
		armorModeButton.setMaximumSize(new Dimension(80,15));
		armorModeButton.setActionCommand("armor mode");
		armorModeButton.addActionListener(this);
		
		JButton cookingModeButton = new JButton("Cooking");
		cookingModeButton.setMaximumSize(new Dimension(80,15));
		cookingModeButton.setActionCommand("cooking mode");
		cookingModeButton.addActionListener(this);
		
		JButton handicraftModeButton = new JButton("Handicraft");
		handicraftModeButton.setMaximumSize(new Dimension(80,15));
		handicraftModeButton.setActionCommand("handicraft mode");
		handicraftModeButton.addActionListener(this);
		
		JButton tailorModeButton = new JButton("Tailor");
		tailorModeButton.setMaximumSize(new Dimension(80,15));
		tailorModeButton.setActionCommand("tailor mode");
		tailorModeButton.addActionListener(this);
		
		JButton weaponModeButton = new JButton("Weapon");
		weaponModeButton.setMaximumSize(new Dimension(80,15));
		weaponModeButton.setActionCommand("weapon mode");
		weaponModeButton.addActionListener(this);
		
		modeChoosePanel.add(alchemyModeButton);
		modeChoosePanel.add(armorModeButton);
		modeChoosePanel.add(cookingModeButton);
		modeChoosePanel.add(handicraftModeButton);
		modeChoosePanel.add(tailorModeButton);
		modeChoosePanel.add(weaponModeButton);
		
		//##########################################
		//  Common Controls (tax rate, etc.)
		//##########################################
		
		JPanel commonPanel = new JPanel();
		commonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		JLabel taxRateLabel = new JLabel("Tax rate:");
		
		taxRateField.setText("10");
		taxRateField.setPreferredSize(new Dimension(30,20));
		taxRateField.getDocument().addDocumentListener(this);
		
		commonPanel.add(taxRateLabel);
		commonPanel.add(taxRateField);
		commonPanel.add(new JLabel("%   "));
		
		JLabel serverPriceLabel = new JLabel("\"Prices\":");
		
		serverPriceField.setText("100");
		serverPriceField.setPreferredSize(new Dimension(30,20));
		serverPriceField.getDocument().addDocumentListener(this);
		
		commonPanel.add(serverPriceLabel);
		commonPanel.add(serverPriceField);
		commonPanel.add(new JLabel("%"));
		
		
		
		//##########################################
		//  Recipe Selector
		//##########################################
		
		JPanel recipePanel = new JPanel();
		recipePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		recipePanel.setPreferredSize(new Dimension(410,160));
		
		browsePanel.init(1);
		recipePanel.add(browsePanel);
		
		//##########################################
		//  Main Panel
		//##########################################
		
		JTabbedPane mainPanel = new JTabbedPane();
		
		scPanel = new SubComponentsPanel();
		browsePanel.setSubcomponentPanel(scPanel);
		scPanel.init();
		scPanel.setPreferredSize(new Dimension(410, 400));
		mainPanel.addTab("Sub components", scPanel);
		
		OrderPanel orderPanel = new OrderPanel();
		orderPanel.setPreferredSize(new Dimension(410, 400));
		mainPanel.addTab("Current Order", orderPanel);
		
		ProfitPanel profitPanel = new ProfitPanel();
		profitPanel.setPreferredSize(new Dimension(410, 400));
		mainPanel.addTab("Profit Calc", profitPanel);
		
		
		//##########################################
		//  Finish Frame Setup
		//##########################################
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
		contentPane.add(modeChoosePanel, BorderLayout.CENTER);
		contentPane.add(new JSeparator(SwingConstants.HORIZONTAL));
		contentPane.add(commonPanel, BorderLayout.CENTER);
		contentPane.add(new JSeparator(SwingConstants.HORIZONTAL));
		contentPane.add(recipePanel, BorderLayout.CENTER);
		contentPane.add(mainPanel, BorderLayout.CENTER);
		contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		frame.getContentPane().add(contentPane);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String sender = e.getActionCommand();
		if (sender.equals("alchemy mode")){
			browsePanel.init(1);
			browsePanel.revalidate();
			browsePanel.repaint();
		} else if (sender.equals("armor mode")){
			browsePanel.init(2);
			browsePanel.revalidate();
			browsePanel.repaint();
		} else if (sender.equals("cooking mode")){
			browsePanel.init(3);
			browsePanel.revalidate();
			browsePanel.repaint();
		} else if (sender.equals("handicraft mode")){
			browsePanel.init(4);
			browsePanel.revalidate();
			browsePanel.repaint();
		} else if (sender.equals("tailor mode")){
			browsePanel.init(5);
			browsePanel.revalidate();
			browsePanel.repaint();
		} else if (sender.equals("weapon mode")){
			browsePanel.init(6);
			browsePanel.revalidate();
			browsePanel.repaint();
		}
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		if(e.getDocument() == taxRateField.getDocument()){
			try{
				scPanel.setTaxRate(Integer.parseInt(taxRateField.getText()));
			} catch (NumberFormatException ex){
				return;
			}
		}
		else if(e.getDocument() == serverPriceField.getDocument()){
			try{
				scPanel.setServerPrices((Integer.parseInt(serverPriceField.getText())));
			} catch (NumberFormatException ex){
				return;
			}
		}
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		if(e.getDocument() == taxRateField.getDocument()){
			try{
				scPanel.setTaxRate(Integer.parseInt(taxRateField.getText()));
			} catch (NumberFormatException ex){
				return;
			}
		}
		else if(e.getDocument() == serverPriceField.getDocument()){
			try{
				scPanel.setServerPrices((Integer.parseInt(serverPriceField.getText())));
			} catch (NumberFormatException ex){
				return;
			}
		}
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		if(e.getDocument() == taxRateField.getDocument()){
			try{
				scPanel.setTaxRate(Integer.parseInt(taxRateField.getText()));
			} catch (NumberFormatException ex){
				return;
			}
		}
		else if(e.getDocument() == serverPriceField.getDocument()){
			try{
				scPanel.setServerPrices((Integer.parseInt(serverPriceField.getText())));
			} catch (NumberFormatException ex){
				return;
			}
		}
	}

}
